package cn.yi.medicalsys.dao.impl;
/**
 * 实现查询病例接口
 */
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javassist.compiler.ast.ArrayInit;
import cn.yi.medicalsys.dao.CaseDao;
import cn.yi.medicalsys.pojo.Case;
import cn.yi.medicalsys.utils.BaseDao;

public class CaseDaoImpl extends BaseDao implements CaseDao {

	@Override
	public List<Case> findAllCase() throws Exception {
		List<Case> list=new ArrayList<Case>();
		String sql="SELECT cid,uid,doctorName,createDate,content,suggest,furtherDate FROM `case`";
		ResultSet rs=super.executeQuery(sql, null);
		while (rs.next()) {
			Case cases=new Case();
			cases.setuId(rs.getInt("uid"));
			cases.setDoctorName(rs.getString("doctorName"));
			cases.setCreateDate(rs.getDate("createDate"));
			cases.setContent(rs.getString("content"));
			cases.setSuggest(rs.getString("suggest"));
			cases.setFurtherDate(rs.getDate("furtherDate"));
			list.add(cases);
		}
		super.closeResource(rs);
		return list;
	}

	@Override
	public int findTotalRecord() throws Exception {
		int row=0;
		List<Object> paramsLsit=new ArrayList<Object>();
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT COUNT(1) FROM `case` ");
		ResultSet rs=super.executeQuery(sql.toString(), null);
		if (rs!=null && rs.next()) {
			row=rs.getInt(1);
		}
		super.closeResource(rs);
		return row;
	}

	@Override
	public List<Case> findCaseByOffset(int offSet, int pageSize) throws Exception {
		List<Case> list = new ArrayList<Case>();
		List<Object> paramsList=new ArrayList<Object>();
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT cid,uid,doctorName,createDate,content,suggest,furtherDate");
		sql.append(" FROM `case` where 1=1 ");
		sql.append(" ORDER BY createDate DESC LIMIT ?,? ");
		paramsList.add(offSet);
		paramsList.add(pageSize);
		Object[] params=paramsList.toArray();
		ResultSet rs = super.executeQuery(sql.toString(), params);
		while (rs != null && rs.next()) {
			Case cases = new Case();
			cases.setcId(rs.getInt("cid"));
			cases.setuId(rs.getInt("uid"));
			cases.setDoctorName(rs.getString("doctorName"));
			cases.setCreateDate(rs.getDate("createDate"));
			cases.setContent(rs.getString("content"));
			cases.setSuggest(rs.getString("suggest"));
			cases.setFurtherDate(rs.getDate("furtherDate"));
			list.add(cases);
		}
		super.closeResource(rs);
		return list;
	}
	public static void main(String[] args) {
		CaseDaoImpl sh=new CaseDaoImpl();
		try {
			System.out.println(sh.findCaseByOffset(0, 3));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Case findCaseByNo(int cId) throws Exception {
		Case cases=null;
		String sql="SELECT cid,uid,doctorName,createDate,content,suggest,furtherDate FROM `case` where cid=?";
		Object[] params={cId};
		ResultSet rs = super.executeQuery(sql.toString(), params);
		while (rs!=null && rs.next()) {
			cases=new Case();
			cases.setcId(rs.getInt("cid"));
			cases.setContent(rs.getString("content"));
			cases.setCreateDate(rs.getDate("createDate"));
			cases.setDoctorName(rs.getString("doctorName"));
			cases.setSuggest(rs.getString("suggest"));
			cases.setFurtherDate(rs.getDate("furtherDate"));
		}
		return cases;
	}
}
